{% extends 'subpage/base.html' %}
{% block title %}
    三线
{% endblock %}
{% block scripts %}
    {{ super() }}
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.config.js') }}"></script>
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/ueditor.all.min.js') }}"></script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/My97DatePicker/WdatePicker.js") }}"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8"
            src="{{ url_for('static', filename='ueditor/lang/zh-cn/zh-cn.js') }}"></script>
    <script src="{{ url_for('static',filename='ueditor/ueditor.parse.js') }}"></script>
    <script>
        setTimeout(function () {
            uParse('div',
                {
                    'highlightJsUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCore.js") }}',
                    'highlightCssUrl': '{{ url_for("static",filename="ueditor/third-party/SyntaxHighlighter/shCoreDefault.css") }}'

                })
        }, 300);
    </script>
    <script type="text/javascript"
            src="{{ url_for("static",filename="extra/myAlert/myAlert.min.js") }}"></script>
{% endblock %}
{% block time_field %}
    <div class="time_field">
        <label for="dutyTime" class="control-label col-xs-4" style="padding-top: 6px;padding-left: 0"><i
                class="require">*</i>日报时间</label>
        <div class="col-xs-8">
            <input type="text" id="dutyTime" class="Wdate"
                   onclick="WdatePicker({maxDate:'%y-%M-%d',dateFmt:'yyyy/MM/dd',alwaysUseStartDate:true})"/>
        </div>
    </div>
{% endblock %}
{% block table %}
    <div class="input-items">
        <form action="javascript:;">
            <div class="form-group clearfix">
                <div class="col-xs-6">
                    <label for="third-onduty" class="control-label col-xs-4"><i class="require">*</i>值班三线</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="third-onduty"/>
                    </div>
                </div>
                <div class="col-xs-6">
                    <label for="forth-onduty" class="control-label col-xs-4"><i class="require">*</i>值班四线</label>
                    <div class="col-xs-8">
                        <input type="text" class="form-control" id="forth-onduty"/>
                    </div>
                </div>
            </div>
            <div class="form-group clearfix">
                <div class="col-xs-6">
                    <label for="brokeContent" class="control-label col-xs-4"><i class="require">*</i>故障内容</label>
                    <div class="col-xs-8">
                        <textarea name="brokeContent" id="brokeContent" class="form-control"></textarea>
                    </div>
                </div>
                <div class="col-xs-6">
                    <label for="brokeDesc" class="control-label col-xs-4"><i class="require">*</i>故障说明</label>
                    <div class="col-xs-8">
                        <textarea name="brokeDesc" id="brokeDesc" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <div class="form-group clearfix">
                <div class="col-xs-6">
                    <label for="brokeTime" class="control-label col-xs-4"><i class="require">*</i>时间</label>
                    <div class="col-xs-8">
                        <input type="text" id="brokeTime" class="Wdate"
                               onclick="WdatePicker({maxDate:'%y-%M-%d 23:59',dateFmt:'yyyy/MM/dd HH:mm',alwaysUseStartDate:true})"/>
                    </div>
                </div>
                <div class="col-xs-6">
                    <button class="btn btn-info three-add-table">添加</button>
                </div>
            </div>
        </form>
    </div>
    <div class="show-input-items clearfix">
        <table class="table table-bordered">
            <tbody>
            <tr>
                <th>时间</th>
                <th>故障内容</th>
                <th>故障说明</th>
                <th>操作</th>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="form-group" style="overflow: hidden;padding: 0 20px">
        <button type="button" class="btn btn-info btn-submit-confirm pull-left">提交</button>
        <button type="button" class="btn btn-default pull-right cancel-return">取消</button>
    </div>

    <script>
        $(function () {
            var index = 0, trs = $('.table tbody tr').length, total = 0, dutyId = GetQueryString("dutyId"),
                addArray = [], deleteArray = [];

            if (dutyId) {//编辑状态
                ajaxRequest("GET", GLOBAL_VARIABLE.BASE_URL + '/datashow', {
                    "dutyId": dutyId,
                    "departId": GetQueryString("departId"),
                    "level": 3
                }, function (data) {
                    $("#third-onduty").val(data.onDutyThirdName);
                    $("#forth-onduty").val(data.onDutyForthName);
                    $("#dutyTime").val(data.richTextTime);
                    toTableVal(data.tableList);
                }, function (error) {
                    $.myAlert({title: "出错提示", message: "请求出错！"});
                });
            }
            function toTableVal(list) {
                if (list.length <= trs - 1) {
                    for (var i = 0; i < list.length; i++, index++, total++) {
                        var broke_time = list[i].brokeTime, broke_content = list[i].brokeContent,
                            broke_deal = list[i].brokeDeal, event_id = list[i].eventId;
                        $(".table tbody tr").eq(i + 1).attr({
                            "data-eventid": event_id
                        }).html('<td>' + broke_time + '</td><td>' + broke_content + '</td><td>' + broke_deal + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                    }
                } else {
                    for (var j = 0; j < list.length; j++, index++, total++) {
                        var broke_time = list[i].brokeTime, broke_content = list[i].brokeContent,
                            broke_deal = list[i].brokeDeal, event_id = list[i].eventId;
                        if (index < trs - 1) {
                            $(".table tbody tr").eq(i + 1).attr({
                                "data-eventid": event_id
                            }).html('<td>' + broke_time + '</td><td>' + broke_content + '</td><td>' + broke_deal + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                        } else {
                            $(".table tbody").append('<tr data-eventid="' + event_id + '"><td>' + broke_time + '</td><td>' + broke_content + '</td><td>' + broke_deal + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>');
                        }
                    }
                }
            }

            //动态标题
            var title = GetQueryString("departAlias") + "&nbsp;&nbsp;&nbsp;三线值班报告";//如果编辑状态的话应该放在ajax success回调的方法下
            $('.title-text').html(title);
            //执行添加按钮
            $(document).on('click', '.three-add-table', function () {
                var brokeContent = $('#brokeContent').val(),
                    brokeDesc = $('#brokeDesc').val(), brokeTime = $('#brokeTime').val();
                if (brokeContent && brokeDesc && brokeTime && brokeContent.length > 0 && brokeDesc.length > 0 && brokeTime.length > 0 && !brokeContent.match(/^[ ]+$/) && !brokeDesc.match(/^[ ]+$/) && !brokeTime.match(/^[ ]+$/)) {
                    if (index < trs - 1) {
                        //清空数据
                        $('#brokeContent').val('');
                        $('#brokeDesc').val('');
                        $('#brokeTime').val('');
                        var change_trs = $(".table tr").length;//可变的表格条数
                        if (change_trs - 1 > index) {
                            $('.table tbody tr').eq(index + 1).html('<td>' + brokeTime + '</td><td>' + brokeContent + '</td><td>' + brokeDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td>');
                        } else {
                            $('.table tbody').append('<tr><td>' + brokeTime + '</td><td>' + brokeContent + '</td><td>' + brokeDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>');
                        }
                        index++;
                        total++;
                    } else {
                        //清空数据
                        $('#brokeContent').val('');
                        $('#brokeDesc').val('');
                        $('#brokeTime').val('');
                        var str = '<tr><td>' + brokeTime + '</td><td>' + brokeContent + '</td><td>' + brokeDesc + '</td><td class="text-center"><i class="fa fa-minus-circle remove-item"></i></td></tr>';
                        $('.table tbody').append(str);
                        total++;
                    }
                } else {
                    $.myAlert({title: "错误信息", message: "参数请提交完整!"});
                }

            });
            //提交数据
            $('.btn-submit-confirm').click(function () {
                var departId = GetQueryString('departId'), departName = GetQueryString('departName'), datalist = [],
                    data = {}, duty_time = $("#dutyTime").val();
                //获取表格数据以及二三线
                var onDutyThirdname = $('#third-onduty').val(), onDutyForthname = $('#forth-onduty').val(),
                    table = $('.table tbody tr');

                //判断是否日报时间为空
                if (!duty_time || duty_time.length === 0) {
                    $.myAlert({title: ' 出错提示', message: '请填写日报时间！'});
                    return false;
                }
                if (onDutyThirdname.replace(/^\s+|\s+$/g, '').length === 0) {
                    $.myAlert({title: "出错提示", message: "请输入值班三线！"});
                    return;
                }
                if (onDutyForthname.replace(/^\s+|\s+$/g, '').length === 0) {
                    $.myAlert({title: "出错提示", message: "请输入值班四线！"});
                    return;
                }
                for (var i = 0; i < total; i++) {
                    datalist.push({
                        brokeTime: $(table[i + 1]).find('td').eq(0).html(),
                        brokeContent: $(table[i + 1]).find('td').eq(1).html(),
                        brokeDeal: $(table[i + 1]).find('td').eq(2).html()
                    });
                    if (!$(table[i + 1]).attr("data-eventid")) {//新增的故障事件信息
                        addArray.push({
                            brokeTime: $(table[i + 1]).find('td').eq(0).html(),
                            brokeContent: $(table[i + 1]).find('td').eq(1).html(),
                            brokeDeal: $(table[i + 1]).find('td').eq(2).html()
                        });
                    }
                }
                if (dutyId) {//修改
                    data = {
                        type: "edit",
                        dutyId: dutyId,
                        departId: parseInt(departId),
                        departName: departName,
                        onDutyThirdname: onDutyThirdname,
                        onDutyForthname: onDutyForthname,
                        datalist: {
                            plus: addArray,
                            delete: deleteArray
                        },
                        level: 3,
                        richTextTime: duty_time
                    };
                } else {//创建
                    data = {
                        type: "create",
                        departId: parseInt(departId),
                        departName: departName,
                        onDutyThirdname: onDutyThirdname,
                        onDutyForthname: onDutyForthname,
                        datalist: JSON.stringify(datalist),
                        level: 3,
                        richTextTime: duty_time
                    };
                }
                $.ajax({
                    type: "POST",
                    url: GLOBAL_VARIABLE.BASE_URL + '/thirddatasave',
                    data: JSON.stringify(data),
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        console.log(data);
                        if (data.code === 0) {//提交成功
                            var dutyId = GetQueryString("dutyId") || data.dutyId, title = GetQueryString('departAlias');
                            window.location.href = '/viewThird?dutyId=' + dutyId + '&departId=' + departId + '&departAlias=' + title + '&duty_time=' + duty_time;
                        } else {
                            $.myAlert({title: '出错提示', message: data.errmessage});
                        }
                    },
                    error: function (ero) {
                        $.myAlert({title: "出错提示", message: "请求出错！"});
                    }
                });

            });
            $(document).on("click", ".remove-item", function () {
                var cur_parentobj = $(this).closest("tr"), event_id = cur_parentobj.attr("data-eventid");
                if (dutyId && event_id) {
                    deleteArray.push(event_id);
                }
                cur_parentobj.remove();
                //当前表格中数据减少一条
                index--;
                total--;
            });
            $(document).on("click", ".cancel-return", function () {
                window.history.go(-1);
            });
        });
    </script>
{% endblock %}